<?
require_once("Hli_Constants.php");
require_once("Hli_RegisterCredentialsType.php");
require_once("Hli_BookingRequestType.php");
require_once("Hli_GetQuoteRequestType.php");
require_once("Hli_ContactType.php");
require_once("Hli_AddressType.php");
require_once("Hli_CollectionType.php");
require_once("Hli_DestinationType.php");
require_once("Hli_ShippingInformationType.php");
require_once("Hli_ErrorType.php");

/**
 * class <b>Hli_Api Class</b>
 * Published 21/12/2010 03:45:13
 *
 * @author Lars Sorhus (lars@saucecode.com.au)
 * @copyright sauceCode Pty Ltd.
 * @version 2.0.4.342.
 * @internal It's not recommended at all to modify this code.
 */
class Hli_Api{
	public $nusoapClient;
	public $nusoapProxy;
	private $__credentials;
	private $__environment; //
	private $__bookingRequest;
	private $__quoteRequest;
   private $__request;
	private $__response;
	private $__debug_str;

	/**
    * Constructor
    */
	public function __construct(){
		$this->__errors=array();
	}

	/**
    * Gets the registration credentials.
    *
	 * @method getRegisterCredentials
	 * @return Hli_RegisterCredentialsType
	 */
	public function getRegisterCredentials(){
		return($this->__credentials);
	}
	/**
    * Sets the registration credentials.
    *
	 * @method setRegisterCredentials
	 * @param Hli_RegisterCredentialsType - The credentials object.
	 * @return void
	 */
	public function setRegisterCredentials(Hli_RegisterCredentialsType $creds){
		$this->__credentials=$creds;
	}

	/**
    * Gets the environment e.g. sandbox / production.
    *
	 * @method getEnvironment
	 * @return Hli_Constants::Env
	 */
	public function getEnvironment(){
		return($this->__environment);
	}
	/**
    * Sets the environment e.g. sandbox / production.
    *
	 * @method setEnvironment
	 * @param Hli_Constants::Env
	 * @return void
	 */
	public function setEnvironment($env){
		$this->__environment=$env;
	}

	/**
    * Gets the booking request member.
    *
	 * @method getBookingRequest
	 * @return Hli_BookingRequestType
	 */
	public function getBookingRequest(){
		return($this->__bookingRequest);
	}
	/**
    * Sets the booking request member.
    *
	 * @method setBookingRequest
	 * @param Hli_BookingRequestType
	 * @return void
	 */
	public function setBookingRequest(Hli_BookingRequestType $bookingRequest){
		$this->__bookingRequest=$bookingRequest;
	}

   /**
    * Gets the quote request member.
    *
	 * @method getQuoteRequest
	 * @return Hli_QuoteRequestType
	 */
	public function getQuoteRequest(){
		return($this->__quoteRequest);
	}

	/**
    * Sets the quote request member.
    *
	 * @method setQuoteRequest
	 * @param Hli_QuoteRequestType
	 * @return void
	 */
	public function setQuoteRequest(Hli_QuoteRequestType $quoteRequest){
		$this->__quoteRequest=$quoteRequest;
	}

	/**
    * Sets the tracking request member.
    *
	 * @method setTrackingRequest
	 * @param Hli_TrackingRequestType
	 * @return void
	 */
	public function setTrackingRequest(Hli_TrackingRequestType $trackingRequest){
		$this->__trackingRequest=$trackingRequest;
	}

   /**
    * Adds a quote request: Sets the flag 'isQuote' to true and passes the request on to the AddBookingRequest
    * which leverages the existing logic and information, to process a quote.
    *
    * @method AddQuoteRequest
    * @param Hli_QuoteRequestType - The quote to be fetched
    * @return Hli_QuoteResponseType
    */
   public function AddQuoteRequest(Hli_QuoteRequestType $quoteRequest){
      $quoteRequest->setIsQuote(true);
      return $this->AddBookingRequest($quoteRequest);
   }

	/**
    * Sends the booking request to the HLI server and receives (if succesful) a response back
    * If the request is of type QuoteRequest, a QuoteResponseType is returned.
    *
	 * @method AddBookingRequest
	 * @param Hli_BookingRequestType - The booking to be placed
	 * @return Hli_BookingResponseType / Hli_QuoteResponseType
	 */
	public function AddBookingRequest(Hli_BookingRequestType $request, $isQuote=false){
		$result=false;
		$gateway=($this->getEnvironment()==Hli_Constants::Env_Production)?Hli_Constants::Gateway_Production:Hli_Constants::Gateway_Sandbox;

      //note: parameter '$request' has been changed to be mandatory!
      //$request=($request===NULL)?$this->__bookingRequest:$request;
      if($isQuote){ $this->setQuoteRequest($request); } else { $this->setBookingRequest($request); }

      $this->nusoapClient=new nusoap_client($gateway.'?wsdl',true);
      $response = ($isQuote) ? new Hli_QuoteResponseType() :  new Hli_BookingResponseType();
      //Check for an error
      $err=$this->nusoapClient->getError();
      if($err){
         $response->addError(new Hli_ErrorType("0xHSD001",$err,Hli_ErrorType::SEVERITY_HIGH));
      }else{
         //Create the proxy
         $this->nusoapProxy=$this->nusoapClient->getProxy();
         $request->setRegisterCredentials($this->__credentials->getCredentials($this->getEnvironment()));
//			print("<h2>Booking Request Data</h2>");
//			print("<pre>");
//			print_r($bookingRequest->serializeToArray());
//			print("</pre>");

         //This AddBookingRequest call processes both Booking & Quote Requests:
         //Check for the flag isQuote on the server side and make sure to return a response
         //of the correct type BookingResponseType/QuoteResponseType
         $responseRaw= ($isQuote) ? $this->nusoapProxy->AddQuoteRequest($request->serializeToArray()) : $this->nusoapProxy->AddBookingRequest($request->serializeToArray());

         $response->setResponseObject($responseRaw);
         if($this->nusoapProxy->fault){
            $response->addError(new Hli_ErrorType("0xHSD002","There was a fault recorded in the booking request. Use the HliBookingResponseType::getNuSoapResponse method to debug this fault.",Hli_ErrorType::SEVERITY_MEDIUM));
         }else{
            // Check for errors
            $err=$this->nusoapProxy->getError();
            if($err){
               $response->addError(new Hli_ErrorType("0xHSD003",$err,Hli_ErrorType::SEVERITY_HIGH));
            }
         }

         $response->setNuSoapRequest($this->nusoapProxy->request);
         $response->setNuSoapResponse($this->nusoapProxy->response);
         $response->setNuSoapDebugOutput($this->nusoapProxy->debug_str);
      }
      $result=$response;
      return($result);

	}

	/**
    * Gets the tracking request: Information on the current status of the order.
    *
	 * @method GetTrackingRequest
	 * @param Hli_TrackingRequestType - The booking to return tracking data for
	 * @return Hli_TrackingResponseType
	 */
	public function GetTrackingRequest(Hli_TrackingRequestType $trackingRequest=NULL){
		$result=false;
		$gateway=($this->getEnvironment()==Hli_Constants::Env_Production)?Hli_Constants::Gateway_Production:Hli_Constants::Gateway_Sandbox;
		$trackingRequest=($trackingRequest===NULL)?$this->__trackingRequest:$trackingRequest;
		$this->setTrackingRequest($trackingRequest);
		$this->nusoapClient=new nusoap_client($gateway.'?wsdl',true);
		$trackingResponse=new Hli_TrackingResponseType();
		//Check for an error
		$err=$this->nusoapClient->getError();
		if($err){
			$trackingResponse->addError(new Hli_ErrorType("0xHSDT00",$err,Hli_ErrorType::SEVERITY_HIGH));
		}else{
			//Create the proxy
			$this->nusoapProxy=$this->nusoapClient->getProxy();
			$trackingRequest->setRegisterCredentials($this->__credentials->getCredentials($this->getEnvironment()));
			$trackingResponseRaw=$this->nusoapProxy->GetTrackingRequest($trackingRequest->serializeToArray());
			$trackingResponse->setResponseObject($trackingResponseRaw);
			if($this->nusoapProxy->fault){
				$trackingResponse->addError(new Hli_ErrorType("0xHSDT01","There was a fault recorded in the tracking request. Use the Hli_TrackingResponseType::getNuSoapResponse method to debug this fault.",Hli_ErrorType::SEVERITY_MEDIUM));
			}else{
				// Check for errors
				$err=$this->nusoapProxy->getError();
				if($err){
					$trackingResponse->addError(new Hli_ErrorType("0xHSDT02",$err,Hli_ErrorType::SEVERITY_HIGH));
				}
			}

			$trackingResponse->setNuSoapRequest($this->nusoapProxy->request);
			$trackingResponse->setNuSoapResponse($this->nusoapProxy->response);
			$trackingResponse->setNuSoapDebugOutput($this->nusoapProxy->debug_str);
		}
		$result=$trackingResponse;
		return($result);
	}
}
?>
